package com.xonm.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HostPingUtils {

	/**
	 * 
	 * @param host
	 * @return -1: MalformedURLException,  -2: IOException
	 */
	public static int host_pings(String host){
	
		int rtnTime = 0;
		
		if (host != null && !host.equals("")) {
			
			try {
				
				host = host.replace("http://", "");
				
				final URL url = new URL("http://" + host);
				
				final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
				urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds
				
				final long startTime = System.currentTimeMillis();
				urlConn.connect();
				final long endTime = System.currentTimeMillis();
				
				if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					
					rtnTime = (int) (endTime - startTime);
					
				}
				
			} catch (MalformedURLException e) {
				rtnTime = -1;
				e.printStackTrace();
			} catch (IOException e) {
				rtnTime = -2;
				e.printStackTrace();
			}

		}
		
		return rtnTime;
	}
	
	
}
